package com.example.zzl.adapter.CartAdapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.model.bean.Cart;
import com.example.zzl.model.bean.Dingdan;
import com.example.zzl.view.activity.MainActivity;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/15.
 */

public class CartLvAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Cart> list;
    private Dingdan dingdan;
    private boolean checked;
    private double sum=0;

    public CartLvAdapter(Context context, ArrayList<Cart> list) {
        this.context = context;
        this.list = list;
        dingdan = new Dingdan();
    }

    public  void setCheck(boolean checked){
        this.checked = checked;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder vh;
        if(view==null){
            view = View.inflate(context, R.layout.cart_lv_item,null);
            vh = new ViewHolder();
            vh.imageView = (ImageView)view.findViewById(R.id.iv_cart_1);
            vh.tv_title = (TextView)view.findViewById(R.id.tv_title);
            vh.tv_content = (TextView)view.findViewById(R.id.tv_content);
            vh.tv_price = (TextView)view.findViewById(R.id.tv_price);
            vh.tv_count = (TextView)view.findViewById(R.id.tv_count);
            vh.tv_add = (TextView)view.findViewById(R.id.tv_add);
            vh.tv_jian = (TextView)view.findViewById(R.id.tv_jian);
            vh.checkBox = (CheckBox)view.findViewById(R.id.ckeckBox);
            view.setTag(vh);
        }else{
            vh = (ViewHolder) view.getTag();
        }
        vh.tv_title.setText(list.get(i).getTitle());
        vh.tv_content.setText(list.get(i).getContent());
        vh.tv_price.setText(""+list.get(i).getPrice());
        vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) {
                    MainActivity.sum += (Double.parseDouble(vh.tv_price.getText().toString()) * Integer.parseInt(vh.tv_count.getText().toString()));
                }else{
                    if(MainActivity.sum>0) {
                        MainActivity.sum -= (Double.parseDouble(vh.tv_price.getText().toString()) * Integer.parseInt(vh.tv_count.getText().toString()));
                    }
                }
            }
        });
        vh.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //vh.tv_count.setText(Integer.parseInt(vh.tv_count.getText().toString())+1);
            }
        });
        vh.tv_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Integer.parseInt(vh.tv_count.getText().toString())>1){
                    //vh.tv_count.setText(Integer.parseInt(vh.tv_count.getText().toString())-1);
                }
            }
        });
        if(checked){
            vh.checkBox.setChecked(true);
        }else{
            vh.checkBox.setChecked(false);
        }
        if(vh.checkBox.isChecked()){
            //dingdan.setContent(vh.tv_title.getText().toString());
            //dingdan.setPrice(Double.parseDouble(vh.tv_price.getText().toString()));
            //dingdan.setCount(Integer.parseInt(vh.tv_count.getText().toString()));
            MainActivity.sum += (Double.parseDouble(vh.tv_price.getText().toString()) * Integer.parseInt(vh.tv_count.getText().toString()));
            //Toast.makeText(context, MainActivity.sum+"加", Toast.LENGTH_SHORT).show();
        }else{
//            dingdan.setContent(null);
//            dingdan.setPrice(0);
//            dingdan.setCount(0);
            if(MainActivity.sum>0) {
                MainActivity.sum -= (Double.parseDouble(vh.tv_price.getText().toString()) * Integer.parseInt(vh.tv_count.getText().toString()));
                //Toast.makeText(context, MainActivity.sum + "减", Toast.LENGTH_SHORT).show();
            }
        }
        return view;
    }


    class ViewHolder{
        ImageView imageView;
        TextView tv_title,tv_content,tv_price,tv_count,tv_add,tv_jian;
        CheckBox checkBox;
    }
}
